package com.team.hiring.company.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.team.hiring.common.Constants;
import com.team.hiring.company.dao.CompanyDao;
import com.team.hiring.company.entity.CompanyEntity;
import com.team.hiring.talent.entity.ResumeEntity;
import com.team.hiring.talent.entity.TalentEntity;


public class CompanyServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    CompanyDao companyDao = new CompanyDao();
    public CompanyServlet() {
        super();
    }
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding(Constants.ENCODE_UTF8);
		String command = request.getParameter("command");
		if("goCompanyLoginUI".equals(command)){
			goCompanyLoginUI(request,response);
		}else if("goCompanyMain".equals(command)){
			goCompanyMain(request,response);
		}else if("goCompanyRegisterUI".equals(command)){
			goCompanyRegisterUI(request,response);
		}else if("register".equals(command)){
			register(request,response);
		}else if("gocompanyDetailUI".equals(command)){
			gocompanyDetailUI(request,response);
		}else if("updateMassageUI".equals(command)){
			updateMassageUI(request,response);
		}else if("updateMassage".equals(command)){
			updateMassage(request,response);
		}else if("updatepasswordUI".equals(command)){
			updatepasswordUI(request,response);
		}else if("updatepassword".equals(command)){
			updatepassword(request,response);
		}else if ("logout".equals(command)) {
			logout(request, response);
		}else if ("homepageCompanyDateilInfo".equals(command)) {
			homepageCompanyDateilInfo(request, response);
		}else if ("goResumeDetailUI".equals(command)) {
			goResumeDetailUI(request, response);
		}else if ("deleteApplication".equals(command)) {
			deleteApplication(request, response);
		}else if ("goHomepageUI".equals(command)) {
			goHomepageUI(request, response);
		}else if ("selectCode".equals(command)) {
			selectCode(request, response);
		}
	}
	
	

	
	
	
	/**
	 * 跳转到企业的登录界面
	 * @param request
	 * @param response
	 */
	private void goCompanyLoginUI(HttpServletRequest request,
			HttpServletResponse response) {
			try {
				request.getRequestDispatcher("/WEB-INF/view/company/companylogin.jsp").forward(request, response);
			} catch (ServletException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
	}
	
	
	/**
	 * 企业登录
	 */
	private void goCompanyMain(HttpServletRequest request,
			HttpServletResponse response) {
		String code = request.getParameter("code");
		String password = request.getParameter("password");
		CompanyEntity companyEntity = companyDao.goCompanyMain(code,password);//携带id和name
		request.getSession().setAttribute("companyEntity", companyEntity);
		request.setAttribute("companyEntity",companyEntity);
		try {
			if(companyEntity.getName()!=null){
				request.getRequestDispatcher("PositionServlet?command=positionList").forward(request, response);
			}else{
				request.getRequestDispatcher("/WEB-INF/view/company/companylogin.jsp?result=fail").forward(request, response);
			}
			} catch (ServletException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	
	/**
	 * 跳转到企业的注册界面
	 * @param request
	 * @param response
	 */
	private void goCompanyRegisterUI(HttpServletRequest request,
			HttpServletResponse response) {
		try {
			request.getRequestDispatcher("/WEB-INF/view/company/companyregister.jsp").forward(request, response);
		} catch (ServletException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	
	/**
	 * 企业注册
	 * @param request
	 * @param response
	 */
	private void register(HttpServletRequest request,
				HttpServletResponse response) {
			String code = request.getParameter("code");
			String password = request.getParameter("password");
			String companyname = request.getParameter("companyname");
			String email = request.getParameter("email");
			String phone = request.getParameter("phone");
			String linkman = request.getParameter("linkman");
			String address = request.getParameter("address");
			int numbers = Integer.parseInt(request.getParameter("numbers"));
			String slogan = request.getParameter("slogan");
			CompanyEntity companyEntity = new CompanyEntity();
			companyEntity.setCode(code);
			companyEntity.setPassword(password);
			companyEntity.setName(companyname);
			companyEntity.setEmail(email);
			companyEntity.setPhone(phone);
			companyEntity.setLinkman(linkman);
			companyEntity.setAddress(address);
			companyEntity.setNumbers(numbers);
			companyEntity.setSlogan(slogan);
			boolean result = companyDao.register(companyEntity);
			if(result){
				try {
					request.getRequestDispatcher("/WEB-INF/view/company/companylogin.jsp").forward(request, response);
				} catch (ServletException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	
	
	/**
	 * 企业详细信息
	 * @param request
	 * @param response
	 */
	private void gocompanyDetailUI(HttpServletRequest request,
			HttpServletResponse response) {
		int companyId = Integer.parseInt(request.getParameter("id"));
 		CompanyEntity companyEntity = companyDao.gocompanyDetailUI(companyId);
 		if(companyEntity!=null){
			try {
				request.setAttribute("companyEntity", companyEntity);
				request.getRequestDispatcher("/WEB-INF/view/company/company-message-detail.jsp").forward(request,response);
			} catch (ServletException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
 		}
	}
	
	
	
	/**
	 * 跳转到修改信息界面
	 * @param request
	 * @param response
	 */
	private void updateMassageUI(HttpServletRequest request,
			HttpServletResponse response) {
		int companyId = Integer.parseInt(request.getParameter("id"));
		CompanyEntity companyEntity = companyDao.updateMassage(companyId);
		if(companyEntity!=null){
			try {
				request.setAttribute("companyEntity", companyEntity);
				request.getRequestDispatcher("/WEB-INF/view/company/update-message.jsp").forward(request,response);
			} catch (ServletException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
 		}
	}
	
	
	/**
	 * 执行修改操作
	 * @param request
	 * @param response
	 */
	private void updateMassage(HttpServletRequest request,
			HttpServletResponse response) {
		CompanyEntity companyEntity = new CompanyEntity();
		int id = Integer.parseInt(request.getParameter("id"));
		String name = request.getParameter("companyname");
		String email = request.getParameter("email");
		String phone = request.getParameter("phone");
		String linkman = request.getParameter("linkman");
		String address = request.getParameter("address");
		int numbers = Integer.parseInt(request.getParameter("numbers"));
		String slogan = request.getParameter("slogan");
		companyEntity.setId(id);
		companyEntity.setName(name);
		companyEntity.setEmail(email);
		companyEntity.setPhone(phone);
		companyEntity.setLinkman(linkman);
		companyEntity.setAddress(address);
		companyEntity.setNumbers(numbers);
		companyEntity.setSlogan(slogan);
		boolean result = companyDao.updateMassage(companyEntity);
		if(result){
			try {
				request.getRequestDispatcher("CompanyServlet?command=gocompanyDetailUI&id="+id).forward(request, response);
			} catch (ServletException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	
	
	/**
	 * 跳转到修改密码界面
	 * @param request
	 * @param response
	 */
	private void updatepasswordUI(HttpServletRequest request,
			HttpServletResponse response) {
		try {
			request.getRequestDispatcher("/WEB-INF/view/company/updatepassword.jsp").forward(request, response);
		} catch (ServletException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
	
	
//	/**
//	 * 输入旧密码，光标移开是触发，查询密码是多少
//	 * @param request
//	 * @param response
//	 */
//	private void oldpasswordSelect(HttpServletRequest request,
//			HttpServletResponse response) {
//		int id = Integer.parseInt(request.getParameter("id"));
//		String aa = request.getParameter("value");
//		System.out.println(aa);
//		String oldpassword = companyDao.oldpasswordSelect(id);
//		if(aa == oldpassword){
//			try {
//				request.getRequestDispatcher("/WEB-INF/view/company/updatepassword.jsp?result=ture").forward(request,response);
//			} catch (ServletException e) {
//				e.printStackTrace();
//			} catch (IOException e) {
//				e.printStackTrace();
//			}
//		}
//	}
	
	
	
	/**
	 * 修改密码
	 * @param request
	 * @param response
	 */
	private void updatepassword(HttpServletRequest request,
			HttpServletResponse response) {
		String newpassword = request.getParameter("newpassword");
		int id = Integer.parseInt(request.getParameter("id"));
		boolean row = companyDao.updatepassword(newpassword,id);
		if(row){
			try {
				request.getRequestDispatcher("/WEB-INF/view/company/companylogin.jsp").forward(request, response);
			} catch (ServletException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	
	
	/**
	 * 注销
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void logout(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//request.getSession().removeAttribute("name");
		request.getSession().invalidate();
		request.getRequestDispatcher("WEB-INF/view/company/companylogin.jsp?command=goCompanyLoginUI").forward(request, response);
	}
	
	
	
	
	/**
	 * 主页点击公司名称后跳转到公司详细页面
	 * @param request
	 * @param response
	 */
	private void homepageCompanyDateilInfo(HttpServletRequest request,
			HttpServletResponse response) {
		int companyId = Integer.parseInt(request.getParameter("companyId"));
		CompanyEntity homepageCompanyDetail = companyDao.homepageCompanyDateilInfo(companyId);
		request.setAttribute("homepageCompanyDetail",homepageCompanyDetail);
		try {
			request.getRequestDispatcher("/WEB-INF/view/common/companyDetailInfo.jsp").forward(request, response);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	
	
	/**
	 * 公司自己页面查看求职者简历
	 * @param request
	 * @param response
	 */
	private void goResumeDetailUI(HttpServletRequest request,
			HttpServletResponse response) {
		int talentId = Integer.parseInt(request.getParameter("talentId"));
		List<TalentEntity> talentEntity = companyDao.goResumeDetailUI(talentId);
		request.setAttribute("talentEntity",talentEntity);
		try {
			request.getRequestDispatcher("/WEB-INF/view/company/resume-detail.jsp").forward(request, response);
		} catch (ServletException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	
	
	
	
	/**
	 * 删除求职信息
	 * @param request
	 * @param response
	 */
	private void deleteApplication(HttpServletRequest request,
			HttpServletResponse response) {
		int applicationId = Integer.parseInt(request.getParameter("applicationId"));
		int result = companyDao.deleteApplication(applicationId);
		if (result == 0) {
			try {
				response.getWriter().print("fail");
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
	}
	
	
	
	
	
	/**
	 * 返回主页
	 * @param request
	 * @param response
	 */
	private void goHomepageUI(HttpServletRequest request,
			HttpServletResponse response) {
		try {
			request.getRequestDispatcher("PositionServlet?command=selectAllPosition&pageNo=1").forward(request, response);
		} catch (ServletException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	
	
	
	
	/**
	 * 注册时查询账号是否存在
	 * @param request
	 * @param response
	 */
	private void selectCode(HttpServletRequest request,
			HttpServletResponse response) {
		String valueCode = request.getParameter("valueCode");
		int result = companyDao.selectCode(valueCode);
		if (result == 0) {
			try {
				response.getWriter().print("yes");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else{
			try {
				response.getWriter().print("no");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	
	
}
